/*
 * Copyright (c) Huawei Technologies Co., Ltd. 2022-2022. All rights reserved.
 *
 */

import { spawn } from 'child_process';
import * as path from 'path';
import { isHvigorLogWithTime } from '../../src/base/util/time-util';

describe('test std-hook', () => {
  test('test isHvigorLogWithTime', () => {
    expect(isHvigorLogWithTime('')).toBeFalsy();
    expect(isHvigorLogWithTime('fjwikrowpe')).toBeFalsy();
    expect(isHvigorLogWithTime('gjiwqoejqwperjqperjqoprjqopeqpeq[eq')).toBeFalsy();
    expect(isHvigorLogWithTime('[2022-08-19T15:54:05.976] > hvigor Finished :feature:PreBuild... after 67 ms'))
      .toBeTruthy();
    expect(isHvigorLogWithTime('a [2022-08-19T15:54:05.976] > hvigor Finished :feature:PreBuild... after 67 ms'))
      .toBeFalsy();
    expect(isHvigorLogWithTime('[2022-08-19T15:54:05.976] > hvigo Finished :feature:PreBuild... after 67 ms'))
      .toBeFalsy();
  });

  test('test initHook', () => {
    const testAppFilePath = path.resolve(
      __dirname,
      './test-init-hook.js',
    );
    const testApp = spawn('node', [testAppFilePath]);

    testApp.stdout.on('data', data => {
      expect(data.toString()).toBe('> hvigor \x1B[32ma\x1B[39m\n');
      testApp.kill('SIGINT');
    });
  });

  test('test unhook', () => {
    const testAppFilePath = path.resolve(
      __dirname,
      './test-unhook.js',
    );
    const testApp = spawn('node', [testAppFilePath]);

    testApp.stdout.on('data', data => {
      expect(data.toString()).toHaveLength(47);
      testApp.kill('SIGINT');
    });
  });
});
